import sys, operator


# 通过是否有账户的三种状态
HAS_COUNT_TRUE = 1
HAS_COUNT_FALSE = 2
HAS_COUNT_INPUT_OTHER_WORD = -1


# 登录中的用户
login_user_list = list()


# 判断有没有账户的输入状态
def check_has_account_sel(sel_str):
    compare_success = False
    compare_str_list = ['y', 'Y', 'n', 'N']
    for compare_str in compare_str_list:
        if operator.eq(sel_str, compare_str):
            compare_success = True
            break

    if compare_success:
        if operator.eq(sel_str, 'y') or operator.eq(sel_str, 'Y'):
            return HAS_COUNT_TRUE
        elif operator.eq(sel_str, 'n') or operator.eq(sel_str, 'N'):
            return HAS_COUNT_FALSE
    else:
        return HAS_COUNT_INPUT_OTHER_WORD


def login(user_info):
    error_time_count = 0
    account_error_time_count = 0
    username_store = '1'
    while True:

        if error_time_count == 3:
            break

        username = input('please enter your username: ')
        username = username.strip()
        if not username:
            print('username can not be empty')
            error_time_count += 1
            continue
        password = input('please enter your password: ')
        password = password.strip()
        if not password:
            print('password can not be empty')
            error_time_count += 1
            continue
        if username and password:
            if username not in user_info:
                print('username or password error')
                error_time_count += 1
                continue
            else:
                # 如果值为-1, 说明已经被锁定
                if operator.eq(user_dict[username][1], '-1'):
                    print('account is locked, please user another one')
                    continue
                if username in login_user_list:
                    print('this account is already login,please user another')
                    continue
                psd = user_dict[username][0]
                if operator.eq(psd, password):
                    print('login successfully\n')
                    print('you can login another account')
                    login_user_list.append(username)
                    error_time_count = 0
                    account_error_time_count = 0
                    continue
                else:
                    # 如果密码输入错误, 帐号错误次数加1,三次锁定
                    print('username or password error')
                    error_time_count += 1
                    account_error_time_count += 1
                    username_store = username

    if error_time_count == 3:
        # 退出时判断是否需要帐号锁定
        if account_error_time_count == 3:
            # 读取所有的行
            line_data = ''
            with open('user.txt', 'r+', encoding='utf-8') as user_file:
                lines = user_file.readlines()
                for line in lines:
                    if username_store in line:
                        line = line.replace('0', '-1')
                    line_data += line
            # 重新写入所有行
            with open('user.txt', 'r+', encoding='utf-8') as user_file:
                user_file.write(line_data)
                print('login incorrect three times, account locked, system exit')
                sys.exit(0)
        else:
            print('login incorrect three times, system exit')
            sys.exit(0)


def register(user_info):
    while True:
        username = input('please enter your username: ')
        username = username.strip()
        if not username:
            print('username can not be empty')
            continue
        if username in user_info:
            print('username exist')
            continue
        password = input('please enter your password: ')
        password = password.strip()
        if not password:
            print('password can not be empty')
            continue
        print('register')
        if username and password:
            with open('user.txt', 'a+', encoding='utf-8') as user_file:
                user_info = username + ',' + password + ',' + '0' + '\n'
                user_file.write(user_info)
                print('create account successfully')
                break


# 将用户数据取出转换成dict,方便查询
user_dict = dict()
with open('user.txt', 'a+', encoding='utf-8') as user_txt:
    user_txt.seek(0, 0)
    lines = user_txt.readlines()
    for line in lines:
        lineData = line.rstrip('\n').split(',')
        user_dict[lineData[0]] = [lineData[1], lineData[2]]

has_account = True
while True:
    has_account_input = input('Do you has a account? enter y or n: ')
    has_account_input = has_account_input.strip()
    status_num = check_has_account_sel(has_account_input)
    if status_num == HAS_COUNT_TRUE:
        has_account = True
        break
    elif status_num == HAS_COUNT_FALSE:
        has_account = False
        break
    else:
        print('you enter a illegal character')

if has_account:
    login(user_dict)
else:
    register(user_dict)








